package cn.wolfcode.crmspringboot.web.controller;

import cn.wolfcode.crmspringboot.query.EmployeeQuery;
import cn.wolfcode.crmspringboot.query.QueryObject;
import cn.wolfcode.crmspringboot.service.impl.UserInfoService;
import cn.wolfcode.crmspringboot.util.JsonResult;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/user")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        model.addAttribute("result",userInfoService.query(qo));
        return "user/list";
    }


    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws Exception {
        //文件下载的响应头
        response.setHeader("Content-Disposition","attachment;filename=user.xls");
        //直接使用poi
        //创建excel文件
        Workbook wb = userInfoService.exportXls();
        //把excel的数据输出给浏览器
        wb.write(response.getOutputStream());
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(String userId){
        userInfoService.deleteUser(userId);
        return new JsonResult();
    }

    @RequestMapping("/status")
    @ResponseBody
    public JsonResult disableOrEnable(String phone,Integer status){
        userInfoService.disableOrEnable(phone,String.valueOf(status));
        return new JsonResult();
    }


}
